package net.technics;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import net.models.Application;
import net.models.Groups;

import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.CLabel;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.DateTime;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import org.eclipse.wb.swt.SWTResourceManager;
import org.eclipse.jface.viewers.ComboViewer;
import org.eclipse.core.databinding.DataBindingContext;
import org.eclipse.jface.databinding.viewers.ObservableListContentProvider;
import org.eclipse.core.databinding.observable.map.IObservableMap;
import org.eclipse.core.databinding.beans.BeansObservables;
import org.eclipse.jface.databinding.viewers.ObservableMapLabelProvider;
import org.eclipse.core.databinding.observable.list.IObservableList;
import org.eclipse.core.databinding.property.Properties;
import org.eclipse.core.databinding.observable.Realm;
import org.eclipse.jface.databinding.swt.SWTObservables;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;

public class MesInformations {
	private DataBindingContext m_bindingContext;

	protected Shell shellMesInformation;
	private Text textNom;
	private Text textPrenom;
	private Text textAdresse;
	private Text textCp;
	private Text textVille;
	private Text txtAjouterUnUtilisateur;
	private List<Groups> groups = new ArrayList<Groups>();
	private Text textdtEmbauche;
	private ComboViewer comboViewerGroupe;

	/**
	 * Launch the application.
	 * 
	 * @param args
	 */
	public static void main(String[] args) {
		Display display = Display.getDefault();
		Realm.runWithDefault(SWTObservables.getRealm(display), new Runnable() {
			public void run() {
				try {
					MesInformations window = new MesInformations();
					window.open();
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Open the window.
	 */
	public void open() {
		groups = Application.getGroups();
		Display display = Display.getDefault();
		createContents();
		shellMesInformation.open();
		shellMesInformation.layout();
		while (!shellMesInformation.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}
	}

	/**
	 * Create contents of the window.
	 */
	protected void createContents() {
		shellMesInformation = new Shell();
		shellMesInformation.setSize(304, 410);

		Composite composite = new Composite(shellMesInformation, SWT.BORDER);
		composite.setBackground(SWTResourceManager.getColor(204, 102, 0));
		composite.setBounds(0, 0, 292, 48);

		txtAjouterUnUtilisateur = new Text(composite, SWT.READ_ONLY);
		txtAjouterUnUtilisateur.setFont(SWTResourceManager.getFont("Segoe UI",
				11, SWT.BOLD));
		txtAjouterUnUtilisateur.setForeground(SWTResourceManager
				.getColor(SWT.COLOR_WHITE));
		txtAjouterUnUtilisateur.setBackground(SWTResourceManager.getColor(204,
				102, 0));
		txtAjouterUnUtilisateur.setText("Mes informations");
		txtAjouterUnUtilisateur.setBounds(10, 10, 178, 21);

		Group grpAdministrateur = new Group(shellMesInformation, SWT.NONE);
		grpAdministrateur.setText("Administrateur");
		grpAdministrateur.setBounds(10, 54, 282, 267);

		CLabel lblNom = new CLabel(grpAdministrateur, SWT.NONE);
		lblNom.setBounds(10, 29, 61, 21);
		lblNom.setText("Nom");

		textNom = new Text(grpAdministrateur, SWT.BORDER);
		textNom.setBounds(141, 22, 124, 28);
		textNom.setEnabled(false);

		textPrenom = new Text(grpAdministrateur, SWT.BORDER);
		textPrenom.setBounds(141, 56, 124, 28);
		textPrenom.setEnabled(false);

		textAdresse = new Text(grpAdministrateur, SWT.BORDER);
		textAdresse.setBounds(141, 90, 124, 28);
		textAdresse.setEnabled(false);

		textCp = new Text(grpAdministrateur, SWT.BORDER);
		textCp.setBounds(141, 124, 124, 28);
		textCp.setEnabled(false);

		textVille = new Text(grpAdministrateur, SWT.BORDER);
		textVille.setBounds(141, 157, 124, 28);
		textVille.setEnabled(false);

		textdtEmbauche = new Text(grpAdministrateur, SWT.BORDER);
		textdtEmbauche.setBounds(140, 192, 125, 26);
		textdtEmbauche.setEnabled(false);


		CLabel lblGroupe = new CLabel(grpAdministrateur, SWT.NONE);
		lblGroupe.setBounds(10, 236, 61, 21);
		lblGroupe.setText("Groupe");

		CLabel lblDateDembauche = new CLabel(grpAdministrateur, SWT.NONE);
		lblDateDembauche.setBounds(10, 197, 124, 21);
		lblDateDembauche.setText("Date d'embauche");

		CLabel lblCodePostal = new CLabel(grpAdministrateur, SWT.NONE);
		lblCodePostal.setBounds(10, 134, 86, 21);
		lblCodePostal.setText("Code postal");

		CLabel lblVille = new CLabel(grpAdministrateur, SWT.NONE);
		lblVille.setBounds(10, 167, 61, 21);
		lblVille.setText("Ville");

		CLabel lblAdresse = new CLabel(grpAdministrateur, SWT.NONE);
		lblAdresse.setBounds(10, 97, 61, 21);
		lblAdresse.setText("Adresse");

		CLabel lblPrnom = new CLabel(grpAdministrateur, SWT.NONE);
		lblPrnom.setBounds(10, 64, 61, 21);
		lblPrnom.setText("Prénom");

		Composite composite_1 = new Composite(shellMesInformation, SWT.NONE);
		composite_1.setBounds(175, 315, 111, 48);

		
		comboViewerGroupe = new ComboViewer(grpAdministrateur, SWT.NONE);
		Combo comboGroupe = comboViewerGroupe.getCombo();
		comboGroupe.setEnabled(false);
		comboGroupe.setBounds(141, 229, 124, 28);
		
		Button btnNewButton = new Button(composite_1, SWT.NONE);
		btnNewButton.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseUp(MouseEvent e) {
				shellMesInformation.close();
			}
		});
		btnNewButton.setBounds(10, 10, 90, 30);
		btnNewButton.setText("Quitter");
		
		textNom.setText(Login.getUserSession().getName());
		textPrenom.setText(Login.getUserSession().getPrenom());
		textAdresse.setText(Login.getUserSession().getAdresse());
		textCp.setText(Login.getUserSession().getCp());
		textVille.setText(Login.getUserSession().getVille());
		textdtEmbauche.setText(String.valueOf(Login.getUserSession().getDateEmbauche()));
		m_bindingContext = initDataBindings();
		
		comboViewerGroupe.setSelection(new StructuredSelection(Login.getUserSession().getGroups()));
	
	}
	protected DataBindingContext initDataBindings() {
		DataBindingContext bindingContext = new DataBindingContext();
		//
		ObservableListContentProvider listContentProvider = new ObservableListContentProvider();
		IObservableMap observeMap = BeansObservables.observeMap(listContentProvider.getKnownElements(), Groups.class, "name");
		comboViewerGroupe.setLabelProvider(new ObservableMapLabelProvider(observeMap));
		comboViewerGroupe.setContentProvider(listContentProvider);
		//
		IObservableList selfList = Properties.selfList(Groups.class).observe(groups);
		comboViewerGroupe.setInput(selfList);
		//
		return bindingContext;
	}
}
